package sample.util;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;


/**

 * @name      :ThreeRowGetPane

 * @Description: three row to get the context
 *
 * @structure
 Pane -> ThreeRowGetPane
     ThreeRowGetPane
     |get(0) -> SplitPane
     |HBox(Text,TextField) |HBox(Text,TextField) |HBox(Text,TextField) |Button(Rectangle)
*/
class ThreeRowGetPane extends Pane {
    //    ObservableList<Text> texts;
//    ObservableList<TextField>getPart;
    public ThreeRowGetPane(){
        super();
    }
    /**

     * @name      :ThreeRowGetPane

     * @param     :[text1, text2, text3, field1, field2, field3]

     * @return    :

     * @Description: you can choose the way you like to create a triple row pane

    */
    public ThreeRowGetPane(Text text1, Text text2, Text text3, TextField field1, TextField field2, TextField field3){
        this(text1,field1,text2,field2,text3,field3);
    }
    public ThreeRowGetPane(Text text1,TextField field1,Text text2,TextField field2,Text text3,TextField field3){
        super();
        field1.setEditable(true);
        field2.setEditable(true);
        field3.setEditable(true);
        SplitPane sp=new SplitPane();
        sp.setOrientation(Orientation.VERTICAL);
        // Button set
        Rectangle rect=new Rectangle(15,15);
        Button button=new Button("Move here",rect);
        button.setContentDisplay(ContentDisplay.RIGHT);
        button.setOnMouseMoved(e-> ThreeRowGetPane.ButtonFillRefresh(button,this));
        sp.getItems().addAll(new HBox(text1,field1),new HBox(text2,field2),new HBox(text3,field3),button);
        this.getChildren().add(sp);
    }
    public String getField(int index){
        if(index<0||index>2)return "";
        return ((TextField)((HBox)(((SplitPane)
                (this.getChildren().get(0))).getItems().get(index)))
                .getChildren().get(1)).getText();
    }
    public int getFieldInteger(int index){
        String s=this.getField(index);
        int val;
        try{
            val = Integer.parseInt(s);
        }catch(NumberFormatException ignored){
            val=0;
        }
        return val;
    }
    public void addButtonEvent(EventHandler<ActionEvent> action){
        ((Button)((SplitPane)(this.getChildren().get(0))).getItems().get(3)).setOnAction(action);
    }
    public static Button ButtonFillRefresh(Button bt,ThreeRowGetPane threeRowGetPane){
        ((Rectangle)bt.getGraphic()).setFill(ThreeRowGetPane.GetColorFromPane(threeRowGetPane));
        return bt;
    }
    public static Color GetColorFromPane(ThreeRowGetPane threeRowGetPane){
        int r=threeRowGetPane.getFieldInteger(0);
        int g=threeRowGetPane.getFieldInteger(1);
        int b=threeRowGetPane.getFieldInteger(2);
        return Color.rgb(ColorRGBValidate(r)
                ,ColorRGBValidate(g)
                ,ColorRGBValidate(b));
    }
    public static int ColorRGBValidate(int val){
        //todo validate the value
        return val%256;
    }
}
